package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.entity.BaseResponse;

import com.six666.medicaldevicetestingsystem.entity.MessageList;
import com.six666.medicaldevicetestingsystem.service.MessageList_Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class MessageList_Controller {
    @Autowired
    private MessageList_Service messageList_service;
    @PostMapping("/get_message")
    public BaseResponse<List<MessageList>> get_message(@RequestParam Map<String, String> params) {
        String message_type= params.get("message_type");
        if(message_type==null){
            return new BaseResponse<>(101,null,"参数错误","类型为空");
        }
        List<MessageList> data=new ArrayList<>(messageList_service.get_message_list_by_type(message_type));
        return new BaseResponse<>(data);
    }
    @PostMapping("/create_message")
    public BaseResponse<Boolean> create_message(@RequestParam Map<String, String> params)  {
        int project_id=Integer.parseInt(params.get("project_id"));
        int account_id=Integer.parseInt(params.get("account_id"));
        String message_type= params.get("message_type");
        String message= params.get("message");
        Date date = new Date(System.currentTimeMillis());
        MessageList messageList=new MessageList(0,project_id,account_id,message_type,"Ongoing",date,message);
       try{
           boolean data= messageList_service.create_message(messageList);
           return new BaseResponse<>(data);
       }
        catch(Exception e){
            return new BaseResponse<>(101,null,"参数错误","项目id或账户id不存在");
       }
    }

    @PostMapping("/finish_message")
    public BaseResponse<Boolean> finish_message(@RequestParam Map<String, String> params) {
        int message_id= Integer.parseInt(params.get("message_id"));
        try{
            boolean data=messageList_service.finish(message_id);
            return new BaseResponse<>(data);
        }
        catch (Exception e){
            return new BaseResponse<>(101,null,"参数错误","留言id不存在");
        }
    }

}
